package com.jw.config;

import com.jw.bean.CurrentUser;
import com.jw.model.SysPermission;
import com.jw.model.SysRole;
import com.jw.model.SysUser;
import com.jw.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author jiangwang
 * @Created by 2021/9/26 18:44
 * @Description: TODO
 */
public class MyRealm extends AuthorizingRealm
{

    @Autowired
    private UserService userService;

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //获取当前用户
        CurrentUser currentUser = (CurrentUser) SecurityUtils.getSubject().getPrincipal();
        List<SysRole> roles = currentUser.getRoles();
        List<SysPermission> permissions = currentUser.getPermissions();
        if (!CollectionUtils.isEmpty(roles))
        {
            for (SysRole role : roles)
            {
                //授权角色
                authorizationInfo.addRole(role.getRole());
            }
        }
        if (!CollectionUtils.isEmpty(permissions))
        {
            for (SysPermission permission : permissions)
            {
                //授权权限
                authorizationInfo.addStringPermission(permission.getPermission());
            }
        }
        return authorizationInfo;
    }

    /**
     * 认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
    {
        //当前用户名
        String username = (String) token.getPrincipal();
        SysUser user = userService.findByUsername(username);
        if (user == null)
        {
            return null;
        }
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        //将当前用户的信息放入session中
        session.setAttribute("user", user);
        //获取当前用户的角色
        List<SysRole> roles = userService.findRolesById(user.getUid());
        //获取当前用户所拥有的权限
        List<SysPermission> permissions = userService.findPermissionByRoles(roles);
        CurrentUser currentUser = new CurrentUser();
        currentUser.setUserInfo(user);
        currentUser.setRoles(roles);
        currentUser.setPermissions(permissions);
        return new SimpleAuthenticationInfo(currentUser, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
    }
}
